package short_semester.questionnaire_planet.service.analyse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.FillBlankQuestionDao;
import short_semester.questionnaire_planet.dao.FillBlankQuestionRecordDao;

import short_semester.questionnaire_planet.dao.QuestionnaireDao;
import short_semester.questionnaire_planet.dao.QuestionnaireRecordDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.response.fillInResponse.FillInResponse;
import short_semester.questionnaire_planet.response.fillResponse.OptionRecordResponse;
import short_semester.questionnaire_planet.response.fillResponse.QuestionRecordResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class FillBlankQuestionRecordService {

    @Autowired
    FillBlankQuestionRecordDao fillBlankQuestionRecordDao;

    @Autowired
    QuestionnaireDao questionnaireDao;

    public List<FillInResponse> getResponse(
            QuestionnaireRecord questionnaireRecord, Integer questionnaireId) {
        List<FillInResponse> result = new ArrayList<>();
        Questionnaire questionnaire = questionnaireDao.findById(questionnaireId).get();
        List<FillBlankQuestion> list = fillBlankQuestionDao.findByQuestionnaire(questionnaire);
        for (FillBlankQuestion eachQuestion: list) {
            
            
            Optional<FillBlankQuestionRecord> optional = fillBlankQuestionRecordDao.
                    findByQuestionnaireRecordAndFillBlankQuestion(questionnaireRecord, eachQuestion);
            if (!optional.isPresent()) {
                FillInResponse response = new FillInResponse();
                response.setType("completion");
                response.setIsAnswer(false);
                response.setId(eachQuestion.getId());
                result.add(response);
            }
            else {
                FillInResponse response = new FillInResponse();
                FillBlankQuestionRecord record = optional.get();
                response.setType("completion");
                response.setIsAnswer(true);
                response.setContent(record.getAnswer());
                response.setId(eachQuestion.getId());
                result.add(response);
            }
        }
        return result;
    }

    @Autowired
    FillBlankQuestionDao fillBlankQuestionDao;
    public void addRecord(
            Integer questionId,
            QuestionnaireRecord questionnaireRecord,
            String content
    ) {
        Optional<FillBlankQuestion> fillBlankQuestion = fillBlankQuestionDao.findById(questionId);
        if (!fillBlankQuestion.isPresent())
            return;
        FillBlankQuestion question = fillBlankQuestion.get();
        Optional<FillBlankQuestionRecord> ret = fillBlankQuestionRecordDao.findByQuestionnaireRecordAndFillBlankQuestion(
                questionnaireRecord, question);
        FillBlankQuestionRecord record;
        if (ret.isPresent()) {
            record = ret.get();
        }
        else {
            record = new FillBlankQuestionRecord();
//            question.setCount(question.getCount() + 1);
            record.setFillBlankQuestion(question);
            record.setQuestionnaireRecord(questionnaireRecord);
            fillBlankQuestionDao.save(question);
        }
        record.setAnswer(content);
        fillBlankQuestionRecordDao.save(record);
    }

    public void submitRecord(QuestionnaireRecord questionnaireRecord){
        for (FillBlankQuestionRecord questionRecord : fillBlankQuestionRecordDao.findByQuestionnaireRecord(questionnaireRecord)) {
            if(questionRecord.getAnswer()!=null && !questionRecord.getAnswer().isEmpty()){
                fillBlankQuestionDao.submitQuestion(questionRecord.getFillBlankQuestion().getId());
            }
        }
    }

    //获取题目记录信息
    public QuestionRecordResponse getRecord(QuestionnaireRecord record, Integer questionId){
        //题目基本信息
        FillBlankQuestion question = fillBlankQuestionDao.getById(questionId);
        FillBlankQuestionRecord questionRecord = fillBlankQuestionRecordDao.findByQuestionnaireRecordAndFillBlankQuestion(record, question).get();
        QuestionRecordResponse questionRecordResponse = new QuestionRecordResponse(question,questionRecord);
        
        return  questionRecordResponse;
    }
    
    //计算分数
    public int compute(QuestionnaireRecord record,Integer questionId){
        int grade = 0;
        FillBlankQuestion question = fillBlankQuestionDao.getById(questionId);
        FillBlankQuestionRecord questionRecord = fillBlankQuestionRecordDao.findByQuestionnaireRecordAndFillBlankQuestion(record, question).get();
        if(Objects.equals(question.getCorrectAnswer(), questionRecord.getAnswer()))
            grade+=question.getGrade();
        
        return grade;
    }
}
